clear; close all; addpath('../utilities');

if exist('jacobian.mat','file')~=2
    
    para = [0.011811964656	0.244090504804	0.034225636514	0.035428703735	2.565573269391	0.165210276777	0.298220369064];

    d       = .03;
    spread  = [1-d 1+d];
    G_m     = nan(length(para),length(spread),11);
    
    for i=1:8
        
        for j=1:length(spread)
            
            para_ij = para;
            para_ij(i) = para_ij(i)*spread(j);
            
            G_m(i,j,:) = main_func_SE(para_ij);
            
        end
    end
    
   save jacobian

end

load cov_matrix_infl S_T
W       = pinv(S_T);

load cov_matrix_NW S_T N G_d

load jacobian para G_m spread

dG      = squeeze(G_m(:,2,:) - G_m(:,1,:));
jac     = dG./(para'*(spread(2)-spread(1)));

d       = jac';
var_opt = pinv( d'*pinv(S_T)*d )/N;

a       = d'*W;
var     = pinv(a*d)*a*S_T*a'*pinv(a*d)'/N;

se      = sqrt(diag(var));

row_labels = {'Idiosyncratic volatility level','Idiosyncratic volatility cyclicality','Aggregate volatility level',...
    'Aggregate volatility cyclicality','Drift scaling','Bankruptcy costs level','Bankruptcy costs cyclicality'};

col_labels = {'Parameter','Estimate'};
Opts.Style = 'jf'; Opts.digit='r'; format = '%.4f';
disp(Mat2TexC(para',format,row_labels,col_labels,se,Opts,'SMM Parameter Estimates'))


load model_simulation G_m
G_T = G_d' - G_m';
J   = N*G_T'*pinv((eye(11)-d*pinv(a*d)*a)*S_T*(eye(11)-d*pinv(a*d)*a)')*G_T
